package com.hsfw.pmauthzuul.service.impl;

import com.hsfw.dbmodule.cypher.Neo4jCypher;
import com.hsfw.dbmodule.dao.LoginDao;
import com.hsfw.dbmodule.entity.Role;
import com.hsfw.dbmodule.entity.UserDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created by RAZR on 16:18 2019/3/22.
 */
@Component(value = "UserDetailsServicelmpl")
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    LoginDao loginDao;
    @Override
    public UserDetails loadUserByUsername(String username) {

        List<UserDetail> userDetail = loginDao.findUserByAccountAllStatus(username);
        UserDetail userInfo;
        if (userDetail.size() == 0) {
            throw new UsernameNotFoundException(String.format("%s账户不存在", username));
        }
        userInfo=userDetail.get(0);
//        Role role = loginDao.findRoleByUserAccount(String.valueOf(userDetail.get(0).getAccount()));
        Role role=new Role(String.valueOf(userDetail.get(0).getType()));
        userInfo.setRole(role);
        return userInfo;
    }
}
